package com.cqhfan.core.autoconfigure.mvc.advice;

import com.cqhfan.core.domain.Result;
import com.cqhfan.core.exceptions.BizException;
import com.cqhfan.core.exceptions.DbException;
import com.cqhfan.core.exceptions.ForbiddenException;
import com.cqhfan.core.exceptions.UnAuthorizationException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(BizException.class)
    public Result<String> handleBizException(RuntimeException exception) {
        String message = exception.getMessage();
        log.info("业务异常，message = {}", message);
        return Result.fail(message);
    }

    @ExceptionHandler(DbException.class)
    public Result<String> handleDbException(RuntimeException exception) {
        String message = exception.getMessage();
        log.info("数据库操作异常，message = {}", message);
        return Result.fail(message);
    }

    @ExceptionHandler(UnAuthorizationException.class)
    public Result<String> handleUnAuthorizationException(RuntimeException exception) {
        return Result.unAuthorizationResult();
    }

    @ExceptionHandler(ForbiddenException.class)
    public Result<String> handleForbiddenException(RuntimeException exception) {
        log.info("无权访问接口，访问信息：{}", exception.getMessage());
        return Result.forbiddenResult();
    }

}
